package com.bkgy.controller.acidefficiency.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.Temp;
import com.bkgy.controller.acidefficiency.dao.TbTcm22280EffMapper;
import com.bkgy.controller.acidefficiency.entity.TbTcm22280Eff;
import com.bkgy.controller.acidefficiency.entity.pdo.UseRate;
import com.bkgy.controller.acidefficiency.service.ITbTcm22280EffService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
@RequiredArgsConstructor
public class TbTcm22280EffServiceImpl extends ServiceImpl<TbTcm22280EffMapper, TbTcm22280Eff> implements ITbTcm22280EffService {
    private final TbTcm22280EffMapper mapper;

    @Override
    public ResultData<TbTcm22280Eff> selectStep(String entId) {
        LambdaQueryWrapper<TbTcm22280Eff> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbTcm22280Eff::getEntid, entId);
        TbTcm22280Eff eff = getOne(wrapper);

        int step = 1;
        if (eff != null) {
            if (StringUtils.isNotEmpty(eff.getStraightTimes())) step++;
            if (StringUtils.isNotEmpty(eff.getHeadToCutterTime())) step++;
            if (StringUtils.isNotEmpty(eff.getArriveWaitPosTime())) step++;
        } else {
            eff = new TbTcm22280Eff();
        }
        eff.setStep(step);
        return ResultData.ResultDataSuccess(eff);
    }

    @Override
    public ResultData<UseRate> useRate() {
        Map<String, List<Temp>> crewMap = new HashMap<>();
        String oldKey = null;
        List<Temp> list = mapper.selectUseRateData();
        for (Temp temp : list) {
            String crew = temp.getK();
            if (!crew.equals(oldKey)) {
                //新一波数据，到达重复组边界
                if (crewMap.containsKey(crew)) {
                    break;
                } else {
                    List<Temp> l = new ArrayList<>();
                    l.add(temp);
                    crewMap.put(crew, l);
                    oldKey = crew;
                }
            } else {
                crewMap.get(crew).add(temp);
            }
        }

        List<UseRate> resList = new ArrayList<>();
        for (Map.Entry<String, List<Temp>> entry : crewMap.entrySet()) {
            UseRate res = new UseRate();
            res.setCrew(entry.getKey());
            List<Temp> l = entry.getValue();
            res.setTotalRoll(l.size());
            int plCount = 0, enCount = 0, exCount = 0;
            for (Temp temp : l) {
                if (temp.getC() == 1) plCount++;
                if (temp.getC1() == 1) enCount++;
                if (temp.getC2() == 1) exCount++;
            }
            res.setPlSpeedUse(plCount);
            res.setEnSpeedUse(enCount);
            res.setExSpeedUse(exCount);
            res.setEnRate(BigDecimal.valueOf(enCount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(l.size()), 2, RoundingMode.HALF_UP));
            res.setPlRate(BigDecimal.valueOf(plCount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(l.size()), 2, RoundingMode.HALF_UP));
            res.setExRate(BigDecimal.valueOf(exCount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(l.size()), 2, RoundingMode.HALF_UP));
            resList.add(res);
        }

        //查询周、月头用量
        Calendar cal = Calendar.getInstance();
        for (int i = 0; i < 2; i++) {
            cal.setTime(new Date());
            cal.add(Calendar.DAY_OF_MONTH, i == 0 ? -7 : -30);
            String weekBegin = DateUtil.getDateToString(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
            List<Temp> weekList = mapper.selectUseRateRank(weekBegin);
            UseRate weekRes = new UseRate();
            weekRes.setCrew(i == 0 ? "week" : "month");
            weekRes.setTotalRoll(weekList.size());

            int plCount = 0, enCount = 0, exCount = 0;
            for (Temp temp : weekList) {
                if (temp.getC() == 1) plCount++;
                if (temp.getC1() == 1) enCount++;
                if (temp.getC2() == 1) exCount++;
            }
            weekRes.setPlSpeedUse(plCount);
            weekRes.setEnSpeedUse(enCount);
            weekRes.setExSpeedUse(exCount);
            weekRes.setEnRate(BigDecimal.valueOf(enCount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(weekList.size()), 2, RoundingMode.HALF_UP));
            weekRes.setPlRate(BigDecimal.valueOf(plCount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(weekList.size()), 2, RoundingMode.HALF_UP));
            weekRes.setExRate(BigDecimal.valueOf(exCount).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(weekList.size()), 2, RoundingMode.HALF_UP));
            resList.add(weekRes);
        }

        return ResultData.ResultDataSuccess(resList, resList.size());
    }
}
